import time

from selenium import webdriver
from selenium.common import InvalidArgumentException, WebDriverException, NoSuchElementException, TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from functions import open_browser
from selenium.webdriver.common.keys import Keys


class Base:
    def __init__(self, browser):
        self.driver = browser

    def get(self, url):  # 输入的网址
        try:
            self.driver.get(url)
        except(InvalidArgumentException, WebDriverException):
            print('输入的地址不正确')

    def window_max(self):  # 窗口最大化
        self.driver.maximize_window()

    def windou__size(self, chang, kuan):  # 窗口分辨率
        self.driver.set_window_size(chang, kuan)

    def back(self):  # 浏览器后退
        self.driver.back()

    def forward(self):  # 浏览器前进
        self.driver.forward()

    def refresh(self):  # 刷新浏览器
        self.driver.refresh()

    def title(self):  # 获取页面标题
        self.driver.title()

    def close(self):  # 关闭当前页面
        self.driver.title()

    # 定位
    def find_element(self, locator, timeout=5):  # locator = (By.xxx, value)
        # 定位元素
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        except TimeoutException:
            print('未获取到元素')

    def click(self, locator):  # 鼠标左键
        element = self.find_element(locator)
        if element is not None:
            element.click()
        else:
            print("元素定位失败")

    def context_click(self, locator):  # 鼠标右键
        ac = ActionChains(self.driver)
        element = self.find_element(locator)
        if element is not None:
            ac.context_click()
        else:
            print('定位失败')

    def get_element_text(self, locator):  # 定位文本框
        element = self.find_element(locator)
        if element:
            return element.text

    def double_click(self, locator):  # 鼠标双击
        ac = ActionChains(self.driver)
        element = self.find_element(locator)
        if element is not None:
            ac.double_click()
        else:
            print('定位失败')

    # def drag_and_drop(self, locator,locator1):  # 鼠标拖动
    #     ac = ActionChains(self.driver)
    #     element = self.find_element(locator)
    #     if element is not None:
    #         ac.context_click()
    #     else:
    #         print('定位失败')

    def keys(self, locator, value):  # 输入类容
        element = self.find_element(locator)
        if element is not None:
            element.send.keys(value)
        else:
            print('元素定位失败')







    def get_attribute(self, locator, value):  # 查看定位目标的源代码
        self.value = self.find_element(locator)
        element = self.find_element(locator)
        if element is not None:
            print(element.get_attribute(value))
        else:
            print('元素定位失败')

    def quit(self, seconds=2):  # 关闭浏览器`
        time.sleep(seconds)
        self.driver.quit()


if __name__ == '__main__':
    browser = open_browser()
    tast = Base(browser)

    tast.get('https://www.baidu.com')

    # # tast.windou__size(800, 800)
    # # tast.window_max()
    # # tast.back()
    # # time.sleep(2)
    # # tast.forward()
    # # time.sleep(2)
    # # tast.forward()
    # # time.sleep(2)
    # id1 = [By.ID, 'kw']
    # tast.find_element(id1,3)
    # # time.sleep(3)
    # # tast.context_click(id1)
    # time.sleep(3)
    # tast.get_attribute(id1,'outerHTML')
    tast.quit()
